package com.sicau.tools;

import cn.hutool.http.HttpUtil;
import com.sicau.constants.FileConstants;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

import java.io.File;

public class ResourceDownloadTool {

    @Tool(description = "从给定url上下载资源")
    public String downloadResource(@ToolParam(description = "要下载的资源链接") String url,
                                   @ToolParam(description = "保存后的文件名") String fileName) {
        String saveDir = FileConstants.WORKSPACE_PATH + "/download";
        String savePath = FileConstants.WORKSPACE_PATH + "/download/" + fileName;
        try {
            // 创建目录
            File file = new File(saveDir);
            if (!file.exists()) {
                boolean mkdirs = file.mkdirs();
                if (!mkdirs) {
                    return "创建目录失败";
                }
            }
            // 下载资源
            HttpUtil.downloadFile(url, new File(savePath));
            return "资源下载成功，" + savePath;
        } catch (Exception e) {
            return "创建目录失败";
        }
    }

}